def modify_json_images_videos(json_data, target_value):
    """
    修改comfyui工作流中输入文件的资源名
    """
    def traverse(obj):
        if isinstance(obj, dict):
            new_obj = {}
            for key, val in obj.items():
                # 检查当前键是否为image或video
                if key in ['image', 'video']:
                    # 判断是否为字符串且非列表
                    if isinstance(val, str) and not isinstance(val, list):
                        new_obj[key] = target_value
                    else:
                        new_obj[key] = traverse(val)  # 递归处理子对象
                else:
                    new_obj[key] = traverse(val)  # 递归处理其他键
            return new_obj
        elif isinstance(obj, list):
            return [traverse(item) for item in obj]  # 递归处理列表元素
        else:
            return obj  # 基础类型直接返回
    
    return traverse(json_data)


if __name__ == "__main__":

    json_data = {
        "1": {
            "inputs": {
            "model_name": "RealESRGAN_x2.pth"
            },
            "class_type": "UpscaleModelLoader",
            "_meta": {
            "title": "加载放大模型"
            }
        },
        "2": {
            "inputs": {
            "image": "abnormal_hands.png"
            },
            "class_type": "LoadImage",
            "_meta": {
            "title": "加载图像"
            }
        },
        "3": {
            "inputs": {
            "upscale_model": [
                "1",
                0
            ],
            "image": [
                "2",
                0
            ]
            },
            "class_type": "ImageUpscaleWithModel",
            "_meta": {
            "title": "使用模型放大图像"
            }
        },
        "4": {
            "inputs": {
            "filename_prefix": "ComfyUI",
            "images": [
                "3",
                0
            ]
            },
            "class_type": "SaveImage",
            "_meta": {
            "title": "保存图像"
            }
        }
        }
    print(modify_json_images_videos(json_data,"kkkkkkkk"))
    pass